<?php
$host = @$_POST['host'] ;
$username = @$_POST['username'] ;
$password = @$_POST['password'] ;
$databaseName  = @$_POST['databaseName'] ;
$Year = @$_POST['year'] ;

$matk = @$_POST['matk'] ;
$tentk = @$_POST['tentk'] ;
$dknovn = @$_POST['dknovn'] ;
$dkcovn = @$_POST['dkcovn'] ;
$mode = @$_POST['mode'] ;

$link = mysqli_connect($host,$username,$password,$databaseName) or die("Error " . mysqli_error($link));

if (!mysqli_set_charset($link, "utf8")) {
} else {
}

$sql = "select * from SDTK".$Year ;
$checktable = mysqli_query($link, $sql); 
if($checktable === FALSE)
{
		echo "1";
		return;
}

$succ = FALSE;
if ($mode == "add")
{
	$sql = "select * from SDTK".$Year." where matk = '".$matk."'  LIMIT 1" ;
	$checktable = mysqli_query($link, $sql);
	if ($checktable->num_rows ==0)
		$succ = true;

}
else
{
	$oldmatk = @$_POST['oldmatk'] ;
	if ($matk == $oldmatk ) //ok
	{
		$succ = true;
	}
	else
	{
		$sql = "select * from SDTK".$Year." where matk = '".$matk."'  LIMIT 1" ;
		$checktable = mysqli_query($link, $sql);
		if ($checktable->num_rows ==0)
			$succ = true;

	}
}

if($succ == true)
{
}
else
{
	echo "2";
	return;
}


if ($mode == "add")
{
	$mysql = "INSERT INTO SDTK".$Year."  SET matk='". $matk. "',";
	$mysql .= "  tentk='". $tentk. "', ";
	$mysql .= "  dknovn='". $dknovn. "', ";
	$mysql .= "  dkcovn='". $dkcovn. "' ";
}
else
{
	$mysql = "UPDATE SDTK".$Year."  SET matk='". $matk. "',";
	$mysql .= "  tentk='". $tentk. "', ";
	$mysql .= "  dknovn='". $dknovn. "', ";
	$mysql .= "  dkcovn='". $dkcovn. "' ";
	$mysql .= "  where matk ='". $oldmatk. "' LIMIT 1 ";
}

echo $mysql;
$succ = mysqli_query($link, $mysql);
if ($succ == false)
{
	echo "3";
	return;
}


mysqli_close($link);


?> 

